package com.suitsoft.bdms.web.system;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.suitsoft.bdms.commos.LogAnnotation;
import com.suitsoft.bdms.commos.SyncUserTimer;
import com.suitsoft.bdms.commos.UserHolder;
import com.suitsoft.bdms.entity.account.User;
import com.suitsoft.bdms.entity.system.PointEntity;
import com.suitsoft.bdms.service.imp.ImpManager;
import com.suitsoft.bdms.service.system.PointInfoManager;
import com.suitsoft.bdms.util.StringUtils;

/**
 * 同步子系统用户信息
 *@author Suine
 */
@SuppressWarnings("serial")
public class SyncDataAction extends ActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private ImpManager impManager;
	private PointInfoManager pointInfoManager;
	
	private List<Map<String,Object>> list = null;
	private Map<String,Object> m = null;
	private Long id;
	
	@Override
	public String execute() throws Exception {
		list = impManager.getAllSync();
		return super.execute();
	}
	@LogAnnotation(log = "同步子系统用户信息")
	public void sync(){
		Object viewName;
		try {
			PrintWriter out = Struts2Utils.getResponse().getWriter();
			String msg = "";
			if(id == null || id == 0L){
				msg = "非法操作";
			}
			m = impManager.get(id);
			if(m == null || m.isEmpty()){
				msg = "非法操作";
			}
			viewName = m.get("VIEW_NAME");
			if(viewName == null){
				msg = "此子系统还未设置同步视图！请在同步子系统用户信息管理中设置同步视图";
			}
			try {
				if(StringUtils.isEmpty(msg.toString())){
					impManager.syncUser(viewName.toString());
					
					PointEntity pointEntity = pointInfoManager.get(id);
					User user = new User();
					
					pointEntity.setLastSyncDate(new Date());
					user.setId(UserHolder.getLoginUser().getId());
					pointEntity.setLastSyncUser(user);
					
					pointInfoManager.save(pointEntity);
					
				}
			} catch (Exception e) {
				e.printStackTrace();
				msg = "同步失败!";
			}
			out.print(msg);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void reSetSyncTimer(){
		List<Map<String,Object>> list = impManager.getPointEntityAll();
		SyncUserTimer syncUserTimer = new SyncUserTimer(list);
		syncUserTimer.init();
	}
	
	@Autowired
	public void setImpManager(ImpManager impManager) {
		this.impManager = impManager;
	}
	public List<Map<String, Object>> getList() {
		return list;
	}
	public void setList(List<Map<String, Object>> list) {
		this.list = list;
	}
	public Map<String, Object> getM() {
		return m;
	}
	public void setM(Map<String, Object> m) {
		this.m = m;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@Autowired
	public void setPointInfoManager(PointInfoManager pointInfoManager) {
		this.pointInfoManager = pointInfoManager;
	}
	
}

